﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Mono.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ScintillaNET;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace TextToolbox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.StartPosition = FormStartPosition.CenterScreen;//启动在屏幕中央
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            scintilla1.Text = "";
            scintilla2.Text = "";

            initTextArea(scintilla1);
            initTextArea(scintilla2);
        }



        //
        //初始化TextArea
        //
        private void initTextArea(ScintillaNET.Scintilla scintilla1)
        {
            scintilla1.EolMode = ScintillaNET.Eol.Lf;
            // 
            // TextArea
            // 
            //this.scintilla1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            //this.scintilla1.Dock = System.Windows.Forms.DockStyle.Fill;
            scintilla1.Font = new System.Drawing.Font("Consolas", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            //this.scintilla1.Location = new System.Drawing.Point(0, 0);
            //this.TextArea.Multiline = true;

            //this.TextArea.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            scintilla1.Size = new System.Drawing.Size(400, 376);
            scintilla1.TabIndex = 0;
            //this.TextArea.WordWrap = false;
            //this.scintilla1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            //
            scintilla1.Styles[Style.Default].Font = "Consolas";
            scintilla1.Styles[Style.Default].Size = 18;
            //显示当前行背景颜色
            scintilla1.CaretLineVisible = true;

            InitNumberMargin(scintilla1);
        }

        //
        //初始化显示行号
        //
        private void InitNumberMargin(ScintillaNET.Scintilla scintilla1)
        {
            //ScintillaNET.MarginCollection ms = TextArea.Margins;
            //String msg = "ms.Count=" + ms.Count;
            //foreach (ScintillaNET.Margin m in ms)
            //{
            //    msg+="\n" + m.Width;
            //}
            //MessageBox.Show(msg);

            int NUMBER_MARGIN = 0;
            var nums = scintilla1.Margins[NUMBER_MARGIN];
            nums.Width = 20;
            nums.Type = MarginType.Number;
            nums.Sensitive = true;
            nums.Mask = 0;
        }

        private void pasteButton1_Click(object sender, EventArgs e)
        {
            scintilla1.Text = "";
            scintilla1.Paste();
            scintilla1.CurrentPosition = 0;
            scintilla1.SelectionEnd = 0;
            scintilla1.FirstVisibleLine = 0;
        }

        private void cleanButton2_Click(object sender, EventArgs e)
        {
            scintilla1.Text = "";
        }

        private void urlEncode_Click(object sender, EventArgs e)
        {
            String url = scintilla1.Text;
            string urlEncode = HttpUtility.UrlEncode(url);
            scintilla2.Text = urlEncode;
        }

        private void urlDecode_Click(object sender, EventArgs e)
        {
            String url = scintilla1.Text;

            // HttpUtility.UrlDecode(url, Encoding.UTF8);
            //UrlEncode(url, Encoding.UTF8, isUpper);
            // 解码
            string urlDecode = HttpUtility.UrlDecode(url);

            scintilla2.Text = urlDecode;
        }

        private void toUpper_Click(object sender, EventArgs e)
        {
            //全大写
            scintilla2.Text = scintilla1.Text.ToUpper();
        }

        private void toLower_Click(object sender, EventArgs e)
        {
            //全小写
            scintilla2.Text = scintilla1.Text.ToLower();
        }

        private void text2Copy_Click(object sender, EventArgs e)
        {
            scintilla2.SelectAll();
            scintilla2.Copy();
            scintilla2.ClearSelections();
        }

        static string JsonToXml(string json)
        {
            JObject jsonObject = JObject.Parse(json);
            XElement root = new XElement("Root",
                from property in jsonObject.Properties()
                select new XElement(property.Name, property.Value.ToString()));

            return root.ToString();
        }

        static string XmlToJson(string xml)
        {
            XDocument xdoc = XDocument.Parse(xml);
            string json = JsonConvert.SerializeXNode(xdoc);
            return json;
        }

        private void scintilla1_TextChanged(object sender, EventArgs e)
        {
            this.scintilla1.ScrollWidth = 5;
            this.scintilla1.ScrollWidthTracking = true;
            int totalLines = scintilla1.Lines.Count;
            string strNumber = totalLines.ToString();
            int length = strNumber.Length;
            UpdateNumberMargin(scintilla1, length);
        }

        private void scintilla2_TextChanged(object sender, EventArgs e)
        {
            this.scintilla2.ScrollWidth = 5;
            this.scintilla2.ScrollWidthTracking = true;
            int totalLines = scintilla2.Lines.Count;
            string strNumber = totalLines.ToString();
            int length = strNumber.Length;
            UpdateNumberMargin(scintilla2, length);
        }


        private void UpdateNumberMargin(ScintillaNET.Scintilla scintilla1, int length)
        {
            int NUMBER_MARGIN = 0;
            var nums = scintilla1.Margins[NUMBER_MARGIN];
            if (length > 1)
            {
                nums.Width = length * 15;
            }
            else {
                nums.Width = length * 20;
            }
            
            nums.Type = MarginType.Number;
            nums.Sensitive = true;
            nums.Mask = 0;
        }

        private void removeBlank_Click(object sender, EventArgs e)
        {

            String text = scintilla1.Text;
            text = RemoveWhiteSpaces(text);
            scintilla2.Text = text;
        }

        static string RemoveWhiteSpaces(string input)
        {
            return string.Join("", input.Split('\n').Select(line => line.Trim()).ToArray()).Replace("\n", "");
        }

        static string RemoveWhiteSpaces2(string input)
        {
            // 将多行字符串拆分为单行
            string[] lines = input.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            // 对每行进行 Trim 处理
            var trimmedLines = lines.Select(line => line.Trim());

            // 将处理后的行重新组合成一个字符串
            string result = string.Join(Environment.NewLine, trimmedLines);
            return result;
        }

        private void removeLineBlank_Click(object sender, EventArgs e)
        {
            String text = scintilla1.Text;
            text = RemoveWhiteSpaces2(text);
            scintilla2.Text = text;
        }
    }
}
